package com.beiji.unisearch.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beiji.unisearch.common.BaseResponse;
import com.beiji.unisearch.common.ResultUtils;
import com.beiji.unisearch.model.dto.post.PostQueryRequest;
import com.beiji.unisearch.model.dto.search.SearchRequest;
import com.beiji.unisearch.model.dto.user.UserQueryRequest;
import com.beiji.unisearch.model.entity.Picture;
import com.beiji.unisearch.model.vo.PostVO;
import com.beiji.unisearch.model.vo.SearchVO;
import com.beiji.unisearch.model.vo.UserVO;
import com.beiji.unisearch.service.PictureService;
import com.beiji.unisearch.service.PostService;
import com.beiji.unisearch.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RequestMapping("/search")
@RestController
@Slf4j
public class SearchController {

    @Resource
    private PictureService pictureService;
    @Resource
    private PostService postService;
    @Resource
    private UserService userService;

    @PostMapping(value = "/all")
    public BaseResponse<SearchVO> searchAll(@RequestBody SearchRequest searchRequest
    , HttpServletRequest request) {

        String searchText = searchRequest.getSearchText();
        int current = searchRequest.getCurrent();
        int pageSize = searchRequest.getPageSize();


        Page<Picture> picturePage = pictureService.searchPicture(searchText, current, pageSize);

        PostQueryRequest postQueryRequest = BeanUtil.toBean(searchRequest, PostQueryRequest.class);
        Page<PostVO> postVOPage = postService.searchPost(postQueryRequest, request);

        UserQueryRequest userQueryRequest = new UserQueryRequest();
        userQueryRequest.setUserName(searchText);
        Page<UserVO> userVOPage = userService.searchUser(userQueryRequest, request);

        SearchVO searchVO = new SearchVO();
        searchVO.setUserList(userVOPage.getRecords());
        searchVO.setPostList(postVOPage.getRecords());
        searchVO.setPictureList(picturePage.getRecords());

        return ResultUtils.success(searchVO);

    }


}
